package com.loe.mvp.initer;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.loe.mvp.R;

/**
 * Created by zls on 2016/6/4.
 */
public class DefaultProgress
{
    private String text = "";
    private AlertDialog dialog;

    private static int resLayout = R.layout.default_progress;

    private static float dimAmount = 0.5f;

    private static boolean isBackground = false;

    public DefaultProgress(Context context)
    {
        dialog = new AlertDialog.Builder(context).create();
        dialog.setCanceledOnTouchOutside(false);

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
        {
            @Override
            public void onCancel(DialogInterface dialog)
            {
                dialog.dismiss();
            }
        });
    }

    /**
     * 给Dialog设置提示信息
     */
    public DefaultProgress setText(String text)
    {
        this.text = text;
        return this;
    }

    public static void setResLayout(int resLayout)
    {
        DefaultProgress.resLayout = resLayout;
    }

    public static void setDimAmount(float dimAmount)
    {
        DefaultProgress.dimAmount = dimAmount;
    }

    public static void setIsBackground(boolean isBackground)
    {
        DefaultProgress.isBackground = isBackground;
    }

    public void show()
    {
        if (!dialog.isShowing())
        {
            dialog.show();
            Window window = dialog.getWindow();
            window.setContentView(resLayout);
            // 去掉黑色背景
            window.setDimAmount(dimAmount);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            window.setGravity(Gravity.CENTER);
            window.getDecorView().setPadding(0, 0, 0, 0);
            try
            {
                TextView textView = window.findViewById(R.id.textView);
                if (textView != null)
                {
                    textView.setText(text);
                    textView.setVisibility(text.isEmpty() ? View.GONE : View.VISIBLE);
                }
                View layout = window.findViewById(R.id.layout);
                if (layout != null && !isBackground)
                {
                    layout.setBackgroundColor(Color.TRANSPARENT);
                }
            } catch (Exception e)
            {
            }
        }
    }

    public void show(String msg)
    {
        if (!dialog.isShowing())
        {
            try
            {
                dialog.show();
                Window window = dialog.getWindow();
                window.setContentView(resLayout);
                // 去掉黑色背景
                window.setDimAmount(dimAmount);
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                window.getDecorView().setPadding(0, 0, 0, 0);

                TextView textView = window.findViewById(R.id.textView);
                if (textView != null)
                {
                    textView.setText(msg);
                    textView.setVisibility(msg.isEmpty() ? View.GONE : View.VISIBLE);
                }
                View layout = window.findViewById(R.id.layout);
                if (layout != null && !isBackground)
                {
                    layout.setBackgroundColor(Color.TRANSPARENT);
                }
            } catch (Exception e)
            {
                Log.d("runtime", e.toString());
            }
        }
    }

    public void show(DialogInterface.OnDismissListener listener)
    {
        if (!dialog.isShowing())
        {
            dialog.setOnDismissListener(listener);
            show();
        }
    }

    public void setOnBack(final DialogInterface.OnCancelListener listener)
    {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener()
        {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
            {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                {
                    listener.onCancel(dialog);
                }
                return false;
            }
        });
    }

    public void cancel()
    {
        try
        {
            dialog.cancel();
        } catch (Exception e)
        {
        }
    }

    public boolean isShowing()
    {
        return dialog.isShowing();
    }
}
